package com.bolehui.config;

import io.minio.MinioClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName MinioConfig
 * @Description
 * @createTime 2024/12/2 9:35
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
@Data
@Configuration
@Slf4j
public class MinioConfig {

    @Value("${minio.endpoint:http://127.0.0.1:9000}")
    private String endpoint;
    @Value("${minio.accessKey:name}")
    private String accessKey;
    @Value("${minio.secretKey:password}")
    private String secretKey;
    @Value("${minio.bucketName:yy-real}")
    private String bucketName;

    @Bean
    public MinioClient minioClient(){
        if(!StringUtils.hasText(endpoint) || !StringUtils.hasText(accessKey) || !StringUtils.hasText(secretKey) || !StringUtils.hasText(bucketName)){
            throw new IllegalArgumentException("Minio configuration properties are not set correctly.");
        }
        try {
            MinioClient client = MinioClient.builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();
            log.info("MinioClient created with endpoint: {}", endpoint);
            return client;
        } catch (Exception e) {
            log.error("Failed to create MinioClient", e);
            throw new RuntimeException(e);
        }
    }
}
